package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 李天玉
 * @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
 * @createDate 2025-08-15 14:26:50
 */
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
        implements BaseAttrInfoService {
    @Autowired
    private BaseAttrInfoMapper baseAttrInfoMapper;

    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 根据分类id查询属性列表
     *
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrInfoAndValue(Long category1Id, Long category2Id, Long category3Id) {
        List<BaseAttrInfo> infos = baseAttrInfoMapper.getAttrInfoAndValue(category1Id, category2Id, category3Id);
        return infos;
    }

    /**
     * 保存平台属性
     *
     * @param baseAttrInfo
     */
    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1.保存平台属性名
        baseAttrInfoMapper.insert(baseAttrInfo);
        //2.拿到属性名的 id
        Long id = baseAttrInfo.getId();
        //3.保存平台属性值
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue baseAttrValue : attrValueList) {
            //3.1设置属性值的属性名id
            baseAttrValue.setAttrId(id);
        }
        baseAttrValueService.saveBatch(attrValueList);
    }

    /**
     * 修改平台属性
     *
     * @param baseAttrInfo
     */
    @Override
    public void updateAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1、修改属性名表数据
        baseAttrInfoMapper.updateById(baseAttrInfo);

        //2.修改属性值表数据
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();

        //带id是修改，不带是新增
        //删除哪些？1、查出attrId当时的所有属性值id集合（59，60，61）
        //2、对比前端提交的（59，61）计算出差集。删除差集数据
        //delete* frombase_attr_valuewhere attr_id=12 and id not in（前端提交的集合)
        //3、拿到前端提交的值id集合
        //3.1先移除所有前端未提交的数据
        List<Long> ids = attrValueList.stream().filter(attrValue -> attrValue.getId() != null)
                .map(attrValue -> attrValue.getId())
                .collect(Collectors.toList());
        if (ids.size() > 0) {
            //删除前端没有带来的属性
            LambdaQueryWrapper<BaseAttrValue> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseAttrValue::getAttrId, baseAttrInfo.getId());
            wrapper.notIn(BaseAttrValue::getId, ids);
            baseAttrValueService.remove(wrapper);
        } else {
            //全删
            LambdaQueryWrapper<BaseAttrValue> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseAttrValue::getAttrId, baseAttrInfo.getId());
            baseAttrValueService.remove(wrapper);
        }

        //3.2遍历，有值id的修改，没有的就添加
        attrValueList.stream()
                .forEach(attrValue -> {
                    if (attrValue.getId() == null) {
                        //新增
                        attrValue.setAttrId(baseAttrInfo.getId());
                        baseAttrValueService.save(attrValue);
                    } else {
                        //修改
                        baseAttrValueService.updateById(attrValue);
                    }
                });
    }
}




